package com.wgchao.identityrightsmanagement.query.vo;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAgency;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.mapstruct.AgencyMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @作者：wgchao
 * @时间：20:42 2020/6/14 0014
 * @用途：
 **/
@Component
public class AgencyVoConvert {
    @Autowired
    private AgencyMapper agencyMapper;
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;

    public AgencyVo convert(SysBaseAgency sysBaseAgency){
        AgencyVo agencyVo = agencyMapper.toT(sysBaseAgency);
        if(!StringUtils.isEmpty(sysBaseAgency.getCreator())){
            SysBaseAccount creator = sysBaseAccountRepository.findFirstByUsername(sysBaseAgency.getCreator());
            agencyVo.setCreatorName(creator == null ? null : creator.getName());
        }
        if(!StringUtils.isEmpty(sysBaseAgency.getLastUpdator())){
            SysBaseAccount updator = sysBaseAccountRepository.findFirstByUsername(sysBaseAgency.getLastUpdator());
            agencyVo.setLastUpdatorName(updator == null ? null : updator.getName());
        }
        return agencyVo;
    }
}
